package com.wh.springkafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

public class ConsumerSampleWasu {

    public static final String TOPIC_NAME = "adi_transform_hubeigd";

    public static void main(String[] args) {
        helloWorld();
    }

    private static void helloWorld(){
        Properties properties = new Properties();
        //kafka.bootstrap.servers=125.210.163.28:9092,125.210.163.29:9092,125.210.163.30:9092
        properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"125.210.163.28:9092");
        properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"ADI_VOD_CONSUMER[HuBeiGD]");
        properties.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"true");
        properties.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"1000");
        properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");

        KafkaConsumer<String,String> consumer = new KafkaConsumer(properties);

        consumer.subscribe(Arrays.asList(TOPIC_NAME));
        while (true){
            ConsumerRecords<String,String> records = consumer.poll(10000);
            System.out.println("~~~~~~~~~~~~~~开始消费");
            for (ConsumerRecord<String,String> record :records){
                System.out.printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~offset = %d, key = %s,value=%s%n "
                        ,record.offset(),record.key(),record.value());
            }

        }


    }
}
